package com.bgds.sys.api.project.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bgds.sys.api.project.request.ProjectRequest;
import com.bgds.sys.api.project.response.ProjectExcelItem;
import com.bgds.sys.api.project.response.ProjectItemResponse;
import com.bgds.sys.api.project.service.ProjectItemService;
import com.bgds.sys.module.entity.project.ProjectItem;
import com.bgds.sys.module.mapper.ProjectItemMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chenchao
 * @date 2021/9/15
 */
@Service
public class ProjectItemServiceImpl implements ProjectItemService {

    @Resource
    private ProjectItemMapper projectItemMapper;

    @Override
    public void add(ProjectItem item) {
        projectItemMapper.insert(item);
    }

    @Override
    public IPage<ProjectItemResponse> pageList(ProjectRequest request) {

        return projectItemMapper.list(new Page(request.getPageNum(), request.getPageSize()), request);
    }

    @Override
    public ProjectItemResponse getProjectItem(Long id) {
        return projectItemMapper.detail(id);
    }

    @Override
    public void export(HttpServletResponse response, String ids) {
        List<ProjectItemResponse> list = projectItemMapper.detailAll(StrUtil.isNotBlank(ids) ? Arrays.stream(ids.split(StrUtil.COMMA)).collect(Collectors.toList()) : null);
        List<ProjectExcelItem> data = list.stream().map(project -> Convert.convert(ProjectExcelItem.class, project)).collect(Collectors.toList());
        // 通过工具类创建writer，默认创建xls格式
        ExcelWriter writer = ExcelUtil.getWriter(true);
        //自定义标题别名
        writer.addHeaderAlias("code", "编码");
        writer.addHeaderAlias("type", "类型");
        writer.addHeaderAlias("sequence", "序号");
        writer.addHeaderAlias("area", "区域");
        writer.addHeaderAlias("deviceName", "设备名称");
        writer.addHeaderAlias("manageProject", "管理项目");
        writer.addHeaderAlias("manageNorm", "管理标准");
        writer.addHeaderAlias("accountabilityUnit", "责任单位");
        writer.addHeaderAlias("submissionUnit", "结果提报单位");
        writer.addHeaderAlias("controlRank", "管控级别");
        writer.addHeaderAlias("measuringMethod", "检测方法");
        writer.addHeaderAlias("period", "评价周期");
        writer.addHeaderAlias("measuringAsk", "测量要求");

        writer.addHeaderAlias("measuredResult", "测量结果");
        writer.addHeaderAlias("variationAnalysis", "差异分析");
        writer.addHeaderAlias("isException", "结果状态");

        writer.addHeaderAlias("createUser", "测量人");
        writer.addHeaderAlias("createTime", "测量时间");

        // 设置行宽
        writer.setColumnWidth(1, 40);
        writer.setColumnWidth(2, 40);
        writer.setColumnWidth(3, 40);
        writer.setColumnWidth(4, 40);
        writer.setColumnWidth(5, 40);
        writer.setColumnWidth(6, 40);
        writer.setColumnWidth(7, 40);
        writer.setColumnWidth(8, 40);
        writer.setColumnWidth(9, 40);
        writer.setColumnWidth(10, 40);
        writer.setColumnWidth(11, 40);
        writer.setColumnWidth(12, 40);
        writer.setColumnWidth(13, 40);
        writer.setColumnWidth(14, 40);
        writer.setColumnWidth(15, 40);
        writer.setColumnWidth(16, 40);
        writer.setColumnWidth(17, 40);
        writer.setColumnWidth(18, 40);
        // 设置行高
        writer.setDefaultRowHeight(30);
        // 写出内容
        writer.write(data, true);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + DateUtil.now() + ".xlsx");

        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            writer.flush(out, true);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭writer，释放内存
            writer.close();
        }
        // 关闭输出Servlet流
        IoUtil.close(out);
    }

    @Override
    public List<Map<String, Object>> statistics() {
        return projectItemMapper.getStatistics();
    }
}
